﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class LocationController : AsyncCommonControllerVersion2
    {
        public ActionResult Index(string search)
        {
            return View();
        }

        public async Task<ActionResult> Locations(Search search = null)
        {
            System.Text.StringBuilder filter = new StringBuilder();
            bool search_IS_ALL = false;
           
            if (search != null)
            {               
                if (!String.IsNullOrEmpty(search.EXT_KEY))
                    filter.Append(String.Format(" substringof('{0}', EXT_KEY) eq true or", search.EXT_KEY));
                if (!String.IsNullOrEmpty(search.LOCATION_NAME))
                    filter.Append(String.Format(" substringof('{0}', LOCATION_NAME) eq true or", search.LOCATION_NAME));
                if (!String.IsNullOrEmpty(search.COUNTRY))
                    filter.Append(String.Format(" substringof('{0}', COUNTRY) eq true or", search.COUNTRY));
                if (!String.IsNullOrEmpty(search.CITY))
                    filter.Append(String.Format(" substringof('{0}', CITY) eq true or", search.CITY));
                if (!String.IsNullOrEmpty(search.STREET))
                    filter.Append(String.Format(" substringof('{0}', STREET) eq true or", search.STREET));
                if (!String.IsNullOrEmpty(search.BUILDING_NUMBER))
                    filter.Append(String.Format(" substringof('{0}', BUILDING_NUMBER) eq true or", search.BUILDING_NUMBER));  
                if (!String.IsNullOrEmpty(search.IS_ALL))
                    search_IS_ALL = true;

            }
            var data = await EdexContext.GetLocations(filter.ToString(), search_IS_ALL);
            return PartialView(data.Data);
        }

        public async Task<ActionResult> Location(Guid id)
        {
            var data = await EdexContext.GetLocationDetails(id);
            return View(data.Data.SingleOrDefault());
        }

        [HttpPost]
        public async Task<ActionResult> Save(EdexApiClient.WH_LOCATIONS record)
        {
            if (record.GUID_RECORD == null || record.GUID_RECORD == Guid.Empty)
                await this.EdexContext.AddLocation(record);
            else
                await this.EdexContext.UpdateLocation(record);

            return Json(new EdexApiClient.CommonApiResponse());
        }

        [HttpPost]
        public async Task<ActionResult> Delete(Guid GUID_RECORD)
        {
            await EdexContext.DeleteLocation(GUID_RECORD.ToString());
            return Json(new EdexApiClient.CommonApiResponse());
        }

        public class Search
        {          
            public String LOCATION_NAME { get; set; }
            public String EXT_KEY { get; set; }
            public String COUNTRY { get; set; }
            public String CITY { get; set; }
            public String STREET { get; set; }
            public String BUILDING_NUMBER { get; set; }
            public String IS_ALL { get; set; }       
        }
    }
}
